import 'package:blog_flutter/controller/MenuController.dart';
import 'package:blog_flutter/utils/CommonUtil.dart';
import 'package:blog_flutter/widgets/HeaderIconWidget.dart';
import 'package:blog_flutter/widgets/HeaderTitleWidget.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class HeaderWidget extends StatelessWidget {

  MenuController _menuController = Get.put(MenuController());

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.black,
      width: double.infinity,
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Container(padding: EdgeInsets.all(20),
            constraints: BoxConstraints(maxHeight: 1200),
            color: Colors.black,
            child: Row(
              children: [
                buildLeftMenu(context),
                Text(
                  "我的博客",
                  style: TextStyle(
                   fontWeight: FontWeight.w600,
                   fontSize: 22,
                   color: Colors.red
                  ),
                ),
                Spacer(),
                Obx(() {
                  return buildMiddleRow(context);
                }),
                Spacer(),
                Row(
                  children: [
                    HeaderIconWidget("assets/images/qq_icon.png", () {

                    }),
                    HeaderIconWidget("assets/images/wx_icon.png", () {

                    }),
                   SizedBox(width: 10,),
                   ElevatedButton(onPressed: (){}, child: Text("联系我们 "))
                  ],
                )
              ],
             ),
          ),
          SizedBox(height: 40,),
          Text("一个爱喝茶的程序员", style: TextStyle(fontSize: 22, fontWeight: FontWeight.w600, color: Colors.white ),),
          SizedBox(height: 20,),
          Text("2021年注定是不平凡的一年，作为新一代的码农，我们需要砥砺前行，战胜一切妖魔鬼怪", style: TextStyle(fontSize: 16, fontWeight: FontWeight.w600, color: Colors.white )),
          SizedBox(height: 40,),
          TextButton(onPressed: (){}, child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text("查看更多", style: TextStyle(color: Colors.white),),
              SizedBox(width: 8,),
              Icon(Icons.arrow_forward_ios, size: 15,)
            ],
          )),
          SizedBox(height: 40,),
        ],
      ),
    );
  }



  Widget buildMiddleRow(BuildContext context) {
    if (CommonUtil.getScreenWidth(context) < 1000) {
      return Spacer();
    }
    return Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: List.generate(_menuController.menuList.length, (index) => HeaderTitleWidget(_menuController.menuList[index], _menuController.currentSelectIndex == index, (){
          _menuController.updateSelectIndex(index);
        }))
    );
  }

  Widget buildLeftMenu(BuildContext context) {
    if (CommonUtil.getScreenWidth(context) <= 1000) {
      return IconButton(icon: Icon(Icons.menu, color: Colors.white,), onPressed: () {
        _menuController.homegGlobalKey.currentState.openDrawer();
      });
    }
    return SizedBox();
  }
}

class HeaderTitleWidget extends StatelessWidget {

  final String title;
  final Function onTab;
  final bool isSelect;
  HeaderTitleWidget(this.title, this.isSelect, this.onTab);

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: onTab,
      child: Container(
        margin: EdgeInsets.only(left: 10, right: 10),
        decoration: BoxDecoration(
            border: isSelect ? Border(bottom: BorderSide(color: Colors.red, width: 2)) : null
        ),
        padding: EdgeInsets.symmetric(vertical: 5),
        child: Text(title, style: TextStyle(fontWeight: FontWeight.w500, fontSize: 18, color: Colors.white, height: 1.1),),
      ),
    );
  }

}